/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mysgoog.bot.algo.territory.shape;

import java.util.LinkedList;
import mysgoog.board.Board;

/**
 *
 * @author Wang Xin'an
 */
public class Shape {

	static public Shape specify(LinkedList<Integer> eyes) {
		// TODO
		return null;
	}

	LinkedList<Integer> eyes;
	Integer step = -1;
	Integer affensiveN; // go first
	Integer defensiveN; // follow opponent

	private Shape(LinkedList<Integer> eyes, String info) {
		this.eyes = eyes;
		parse(info);
	}

	private void parse(String info) {
		String[] arr = info.split(" ");
		step = new Integer(arr[0]);
		affensiveN = new Integer(arr[1]);
		defensiveN = new Integer(arr[2]);
	}

	public int affensiveN() {
		return this.affensiveN;
	}

	public int defensiveN() {
		return this.defensiveN;
	}

	public int createPoint(Board brd) {
		if (step >= 0) {
			return this.eyes.get(step);
		} else if (eyes.size() <= 2)
			return -1;
		else {

			float avgx = 0;
			float avgy = 0;
			for (Integer pos : eyes) {
				avgx += brd.posX(pos);
				avgy += brd.posY(pos);
			}
			avgx /= eyes.size();
			avgy /= eyes.size();

			int minpos = -1;
			float mindis = 10000000;

			for (Integer pos : eyes) {
				float tmp = distance(avgx, avgy, pos, brd);
				if (tmp < mindis) {
					mindis = tmp;
					minpos = pos;
				}
			}
			return minpos;
		}
	}

	private float distance(float cx, float cy, int pos, Board brd) {
		int x = brd.posX(pos);
		int y = brd.posY(pos);
		return (cx - x) * (cx - x) + (cy - y) * (cy - y);
	}

}
